package com.admin.system.modules.system.rest;

import com.admin.common.constant.ExceptionConstant;
import com.admin.common.entity.BaseEntity;
import com.admin.common.exception.BadRequestException;
import com.admin.common.utils.PageResult;
import com.admin.core.dto.DictDetailQueryDTO;
import com.admin.core.entity.SysDict;
import com.admin.core.entity.SysDictDetail;
import com.admin.logging.annotation.Log;
import com.admin.logging.enums.OperateTypeEnum;
import com.admin.system.modules.system.service.DictDetailService;
import com.admin.system.modules.system.service.DictService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author sq-lan
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "系统：字典详情管理")
@RequestMapping("/api/dictDetail")
public class DictDetailController {

    private final DictDetailService dictDetailService;
    private final DictService dictService;

    @ApiOperation("查询")
    @GetMapping
    public ResponseEntity<PageResult<SysDictDetail>> paging(DictDetailQueryDTO dto) {
        if(StringUtils.hasText(dto.getDictName())) {
            SysDict dict = dictService.getOne(Wrappers.<SysDict>lambdaQuery().eq(SysDict::getName, dto.getDictName()), false);
            if (dict != null) {
                dto.setDictId(dict.getId());
            }
        }
        return new ResponseEntity<>(dictDetailService.paging(dto), HttpStatus.OK);
    }

    @Log(module = "字典详情管理", operateType = OperateTypeEnum.ADD)
    @ApiOperation("新增字典详情")
    @PostMapping
    @PreAuthorize("@ss.check('dict:add')")
    public ResponseEntity<Object> add(@Validated @RequestBody SysDictDetail entity) {
        if (entity.getId() != null) {
            throw new BadRequestException(ExceptionConstant.ERROR_PARAM);
        }
        dictDetailService.save(entity);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log(module = "字典详情管理", operateType = OperateTypeEnum.MODIFY)
    @ApiOperation("修改字典详情")
    @PutMapping
    @PreAuthorize("@ss.check('dict:edit')")
    public ResponseEntity<Object> modify(@Validated(BaseEntity.Update.class) @RequestBody SysDictDetail entity) {
        dictDetailService.updateById(entity);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log(module = "字典详情管理", operateType = OperateTypeEnum.REMOVE)
    @ApiOperation("删除字典详情")
    @DeleteMapping(value = "/{id}")
    @PreAuthorize("@ss.check('dict:del')")
    public ResponseEntity<Object> remove(@PathVariable int id) {
        dictDetailService.removeById(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
